﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using BusinessFacade;
using EntityManage;
public partial class Modules_Personal_PerfEvaluateOrgPersonalTargetTypeandTargetScore : System.Web.UI.Page
{
    IReportHandler rptHandler = new ReportManage();
    IPerf_Personal_ApprovalHandler approvalHandler = new Perf_Personal_ApprovalManage();
    public ISys_UserHandler userHandler = new Sys_UserManage();
    DataBase db = new DataBase();
    IPerf_Personal_EvaluateDtlHandler evaluateDtlHandler = new Perf_Personal_EvaluateDtlManage();
    IPerf_Personal_EvaluateHandler evaluateHandler = new Perf_Personal_EvaluateManage();
    IPerf_Personal_TargetHandler targetHanler = new Perf_Personal_TargetManage();
    IPerf_Personal_TargetTypeHandler targettypeHandler = new Perf_Personal_TargetTypeManage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["UserId"] != null && Request["UserId"] != string.Empty)
            {
                ViewState["UserID"] = Request["UserId"].ToString();
            }
            if (Request["Month"] != null && Request["Month"] != string.Empty)
            {
                ViewState["Month"] = Request["Month"].ToString();
            }
            ViewState["NowYear"] = DateTime.Now.Year;
            List<SqlParameter[]> parms = null;
            string clause = " 1=1 ";

            ProcessSeasonParms(ref clause, ref parms);
            dataBind(clause, parms);
        }
        
    }
    private void dataBind(string whereClause, List<SqlParameter[]> parms)
    {
        DataTable dtNew = new DataTable();
        DataTable dtTarget = new DataTable();
        dtNew.Columns.Add("指标类型");
        dtNew.Columns.Add("指标类型得分");
        dtNew.Columns.Add("指标名字");
        dtNew.Columns.Add("指标得分");
        if (whereClause != string.Empty)
        {
            #region
            ////根据传过来的日期员工ID获取季度的评分表ID
            //DataTable dtapproval = approvalHandler.GetPerf_Personal_ApprovalList(whereClause, parms);
            //if (dtapproval != null & dtapproval.Rows.Count > 0)
            //{
            //    for (int i = 0; i < dtapproval.Rows.Count; i++)
            //    {
            //        SqlParameter[] evaluateParms = new SqlParameter[1];
            //        SqlParameter[] targettypeParms = new SqlParameter[1];
            //        evaluateParms[0] =new SqlParameter("@Evaluate_ID",dtapproval.Rows[i]["EvaluateIDs"].ToString());
            //        targettypeParms[0] = new SqlParameter("@Evaluate_ID", dtapproval.Rows[i]["EvaluateIDs"].ToString());
            //        DataTable dtEvaluateDtl = evaluateDtlHandler.GetPerf_Personal_EvaluateDtlList(" Evaluate_ID=@Evaluate_ID ", evaluateParms);
            //        DataTable dtTargetType = rptHandler.GetPerTargetTypeTotalGrade("e.id=@Evaluate_ID", targettypeParms);
            //        if (dtEvaluateDtl != null & dtEvaluateDtl.Rows.Count > 0)
            //        {
            //            //循环评分详细表获取指标
            //            foreach (DataRow r in dtEvaluateDtl.Rows)
            //            {
            //                Perf_Personal_TargetQuery targetQuery = targetHanler.GetPerf_Personal_TargetQuery(int.Parse(r["Target_ID"].ToString()));

            //                DataRow row = dtNew.NewRow();
            //                if (dtTargetType != null & dtTargetType.Rows.Count > 0)
            //                {
            //                    foreach (DataRow rowType in dtTargetType.Rows)
            //                    {
            //                        if (rowType["TypeID"].ToString() == targetQuery.TargetType_ID.ToString())
            //                        {
            //                            row["指标类型"] = rowType["TypeName"];
            //                            row["指标类型得分"] = rowType["TotalScore"];
            //                        }
            //                    }
            //                }
            //                row["指标名字"] = targetQuery.Name.ToString();
            //                row["指标得分"] = int.Parse(r["Score"].ToString()) * int.Parse(targetQuery.Score.ToString());
            //                dtNew.Rows.Add(row);
            //            }
            //        }
            //    }
            //}
            #endregion
            dtTarget = rptHandler.GetPerTargetScore(" pa.begin_time between @BegionTime and @EndTime  and pa.Target_User_ID= @Target_User_ID ", parms[0]);
            if (dtTarget != null && dtTarget.Rows.Count > 0)
            {
                for (int i = 0; i < dtTarget.Rows.Count; i++)
                {
                    DataRow row = dtNew.NewRow();
                    row["指标名字"] = dtTarget.Rows[i]["TargetName"];
                    row["指标得分"] = dtTarget.Rows[i]["TotalScore"];
                    row["指标类型"] = GetTargetTypeName(int.Parse(dtTarget.Rows[i]["TargetID"].ToString()));
                    row["指标类型得分"] = dtTarget.Compute("Sum(TotalScore)", "TargetType='" + dtTarget.Rows[i]["TargetType"] + "'");//统计指标类型分数
                    dtNew.Rows.Add(row);
                }
            }
            //this.AspNetPager.RecordCount = counter;
            this.gvPersonalList.DataSource = dtNew;
            this.gvPersonalList.DataBind();
        }
    }
    public string GetTargetTypeName(int targetId)
    {
        Perf_Personal_TargetQuery query = targetHanler.GetPerf_Personal_TargetQuery(targetId);
        Perf_Personal_TargetTypeQuery typeQuery = targettypeHandler.GetPerf_Personal_TargetTypeQuery(query.TargetType_ID);
        return typeQuery.Name;
    }
    protected void gvTargetList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
 
    }
    protected void CreateTarget_Click(object sender, EventArgs e)
    {

    }
    private DataTable getEvaluate(SqlParameter[] prams)//根据季度获取用户评分表数据
    {
        DataTable dtEvaluate = new DataTable();
        string strSql=" select * from dbo.Perf_Personal_Evaluate where Approval_ID in ";
             strSql += "(select id from dbo.Perf_Personal_Approval where Perf_Date between @Begin and @End) and target_user_id=@UserId ";
             return dtEvaluate = db.RunSqlTab(strSql, prams);
    }
   
    /// <summary>
    /// 季度参数获取
    /// </summary>
    /// <param name="whereClause"></param>
    /// <param name="parms"></param>
    private void ProcessSeasonParms(ref string whereClause, ref List<SqlParameter[]> parms)
    {
        List<SqlParameter> lstParm = new List<SqlParameter>();
        parms = new List<SqlParameter[]>();
        //获取季度
        if (int.Parse(ViewState["Month"].ToString()) >= 1 && int.Parse(ViewState["Month"].ToString()) <= 3)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/01/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/03/31"));
        }
        else if (int.Parse(ViewState["Month"].ToString()) >= 4 && int.Parse(ViewState["Month"].ToString()) <= 6)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/04/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/06/30"));
        }
        else if (int.Parse(ViewState["Month"].ToString()) >= 6 && int.Parse(ViewState["Month"].ToString()) <= 9)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/07/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/09/30"));
        }
        else if (int.Parse(ViewState["Month"].ToString()) >= 10 && int.Parse(ViewState["Month"].ToString()) <= 12)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/10/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/12/31"));
        }
        if (ViewState["UserID"].ToString()!=string.Empty)
        {
            whereClause += " and pa.Target_User_ID= @Target_User_ID ";
            lstParm.Add(new SqlParameter("@Target_User_ID", ViewState["UserID"]));
        }
        parms.Add(lstParm.ToArray());
    }
    private DataTable getEvaluateDtl(string whereString,SqlParameter[] prams)//根据季度获取用户评分表数据
    {
        DataTable dtEvaluateDtl = new DataTable();
        dtEvaluateDtl = this.evaluateDtlHandler.GetPerf_Personal_EvaluateDtlList();
        return dtEvaluateDtl;
    }
    protected void gvPersonalList_Sorting(object sender, GridViewSortEventArgs e)
    {

    }
    protected void gvPersonalList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
        }
    }
    /// <summary>
    /// 调用格式化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvPersonalList_PreRender(object sender, EventArgs e)
    {
        MergeRows(this.gvPersonalList);
    }
    /// <summary>
    /// 格式化gridview合并相同单元格
    /// </summary>
    /// <param name="GridView1"></param>
    public static void MergeRows(GridView GridView1)
    {
        for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = GridView1.Rows[rowIndex];

            GridViewRow previousRow = GridView1.Rows[rowIndex + 1];

            for (int i = 0; i < row.Cells.Count; i++)
            {
                if (row.Cells[1].Text == previousRow.Cells[1].Text)
                {
                    row.Cells[1].RowSpan = previousRow.Cells[1].RowSpan < 1 ? 2 : previousRow.Cells[1].RowSpan + 1;

                    previousRow.Cells[1].Visible = false;
                }
                if (row.Cells[0].Text == previousRow.Cells[0].Text)
                {
                    row.Cells[0].RowSpan = previousRow.Cells[01].RowSpan < 1 ? 2 : previousRow.Cells[1].RowSpan + 1;

                    previousRow.Cells[0].Visible = false;
                }
            }
        }
    }
    protected void ApprovalButton_Click(object sender, EventArgs e)
    {

    }
    protected void LBtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("PerfEvaluateOrgPersonalReport.aspx"); 
    }
    protected void CreateButton_Click(object sender, EventArgs e)
    {

    }
    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {

    }
    protected void gvPersonalList_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

    }
}
